const { getLogin } = require("../../api/login");

Page({
  data: {
    userInfo: null,
    loginData: {
      code: "",
      iv: "",
      encryptedData: "",
      signature: "",
    },
  },

  // 获取登录 code
  getcode() {
    const that = this;
    wx.login({
      success(res) {
        that.setData({
          ["loginData.code"]: res.code,
        });
      },
    });
  },



  // 获取用户信息
  getUserProfile(e) {
    const that = this;
    wx.getUserProfile({
      desc: "desc",
      success: (res) => {
        that.setData({
          userInfo: res.userInfo,
          ["loginData.iv"]: res.iv,
          ["loginData.encryptedData"]: res.encryptedData,
          ["loginData.signature"]: res.signature,
        });
        getLogin(that.data.loginData)
          .then((res) => {
            wx.setStorage({
              key: "token",
              data: res.data.data.token,
            });
            wx.switchTab({
              url: "../home/home",
            });
          })
          .catch((err) => {
            console.log("err-->", err);
          });
      },
      file(err) {
        console.log(err);
      },
    });
  },

  // 登录按钮
  loginBtn() {
    this.getUserProfile();
  },

  onShow() {
    this.getcode();
  },
  onLoad() {},
});
